package com.gitee.qdbp.general.system.web.controller.reusable;

import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.gitee.qdbp.able.exception.ServiceException;
import com.gitee.qdbp.able.result.ResponseMessage;
import com.gitee.qdbp.base.annotation.OperateRecord;
import com.gitee.qdbp.base.utils.WebTools;
import com.gitee.qdbp.general.common.api.settings.model.AreaBean;
import com.gitee.qdbp.general.common.api.settings.service.IAreaDivisionQueryer;
import com.gitee.qdbp.general.common.enums.AreaType;
import com.gitee.qdbp.general.thirdpart.api.map.model.IpLocation;
import com.gitee.qdbp.general.thirdpart.api.map.model.SuggestionDetails;
import com.gitee.qdbp.general.thirdpart.api.map.model.TownshipDetails;
import com.gitee.qdbp.general.thirdpart.api.map.service.IMapService;
import com.gitee.qdbp.tools.utils.ConvertTools;
import com.gitee.qdbp.tools.utils.VerifyTools;

/**
 * 地址管理控制器
 *
 * @author zhaohuihua
 * @version 180628
 */
@Controller
@RequestMapping("actions/area-core")
public class AreaCoreController {

    @Autowired
    private IMapService mapService;
    @Autowired
    private IAreaDivisionQueryer areaDivisionQueryer;

    // 判断是不是内部IP
    // 127.0.0.1
    // 192.168.0.0-192.168.255.255
    // 10.0.0.0-10.255.255.255
    // 172.16.0.0-172.31.255.255
    private static boolean isIntranetIp(String ip) {
        if (ip == null) return false;
        if (ip.equals("127.0.0.1")) return true;
        if (ip.startsWith("192.168.")) return true;
        if (ip.startsWith("10.")) return true;
        if (ip.startsWith("172.")) {
            int sidx = ip.indexOf('.');
            int eidx = ip.indexOf('.', sidx + 1);
            if (sidx < eidx) {
                String temp = ip.substring(sidx + 1, eidx);
                int number = ConvertTools.toInteger(temp);
                return number >= 16 && number <= 31;
            }
        }
        return false;
    }

    /** IP定位 **/
    @ResponseBody
    @RequestMapping("ip-locating")
    @OperateRecord(enable = false)
    public ResponseMessage ipLocating() {
        try {
            IpLocation location = null;
            String ip = WebTools.me.getIpAddress();
            if (isIntranetIp(ip)) {
                ip = null;
            }
            IpLocation temp = mapService.ipLocating(ip);
            if (temp != null && VerifyTools.isNotBlank(temp.getLocation())) {
                location = temp;
            }
            return new ResponseMessage(location);
        } catch (ServiceException e) {
            return new ResponseMessage(e);
        }
    }

    /** 查询所有行政区域 **/
    @ResponseBody
    @RequestMapping("area-divisions")
    @OperateRecord(enable = false)
    public ResponseMessage areaDivisions(String sceneType) {
        try {
            List<AreaBean> list = areaDivisionQueryer.listAll(sceneType);
            List<AreaBean> temp = new ArrayList<>();
            for (AreaBean item : list) {
                AreaType type = item.getType();
                if (type == AreaType.PROVINCE || type == AreaType.CITY || type == AreaType.DISTRICT) {
                    temp.add(item);
                }
            }
            return new ResponseMessage(temp);
        } catch (ServiceException e) {
            return new ResponseMessage(e);
        }
    }

    /** 根据用户输入的关键字给出地址提示 **/
    @ResponseBody
    @RequestMapping("suggestion")
    @OperateRecord(enable = false)
    public ResponseMessage suggestion(String keyword, String location) {
        try {
            List<SuggestionDetails> list = mapService.suggestion(keyword, location);
            return new ResponseMessage(list);
        } catch (ServiceException e) {
            return new ResponseMessage(e);
        }
    }

    /** 根据经纬度查询乡镇街道级信息 **/
    @ResponseBody
    @RequestMapping("township")
    @OperateRecord(enable = false)
    public ResponseMessage township(String sceneType, String location) {
        try {
            TownshipDetails details = mapService.township(sceneType, location);
            return new ResponseMessage(details);
        } catch (ServiceException e) {
            return new ResponseMessage(e);
        }
    }

}
